package de.ff7rl.core.map;

import java.awt.Color;
import java.awt.Point;

import de.ff7rl.ui.Terminal;

public class DungeonMapPrintUtils {

  public static void printMap(final DungeonMap theDungeonMap, final Terminal theTerminal) {
    final Point counter = new Point(theDungeonMap.getOffset());
    for (final DungeonMapLine line : theDungeonMap.getLines()) {
      for (final Tile tile : line.getTiles()) {
        printCharacter(theDungeonMap, tile, theTerminal, counter);
        counter.x++;
      }
      counter.x = theDungeonMap.getOffset().x;
      counter.y++;
    }
  }

  private static Color calculateBackGroundColor(final DungeonMap theDungeonMap, final Tile theTile, final Point thePoint) {
    return theTile.getBackGroundColor();
  }

  private static String calculateCharacter(final DungeonMap theDungeonMap, final Tile theTile, final Point thePoint) {
    final Player player = theDungeonMap.getPlayer();
    if (player != null && player.getPosition().x == thePoint.x && player.getPosition().y == thePoint.y) {
      return "@";
    }
    return theTile.getCharacter();
  }

  private static Color calculateForeGroundColor(final DungeonMap theDungeonMap, final Tile theTile, final Point thePoint) {
    return theTile.getForeGroundColor();
  }

  private static void printCharacter(final DungeonMap theDungeonMap, final Tile theTile, final Terminal theTerminal,
      final Point thePoint) {
    final String character = calculateCharacter(theDungeonMap, theTile, thePoint);
    final Color foregroundColor = calculateForeGroundColor(theDungeonMap, theTile, thePoint);
    final Color backgroundColor = calculateBackGroundColor(theDungeonMap, theTile, thePoint);
    theTerminal.write(thePoint.x, thePoint.y, character, foregroundColor, backgroundColor);
  }

}
